﻿// This function initializes the map along with the markers and related information


var map;

var latlng = new google.maps.LatLng(40.745575, -73.990855);


function initialize() {
    var geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(40.745575, -73.990855);
    var myOptions = {
        zoom: 10,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

function codeAddress() {
    alert("CodeDecode");
    var address = document.getElementById("address").value;
    alert(address);
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': address }, function (results, status) {
        alert("here");
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

function Bt_SearchByCity_onclick() {
    var address = document.getElementById("address").value;
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            map.setZoom(10);
        }
    });
}

function init_map_open(map_canvas_id, markers, infoWindowContents, Bound) {

    var address = document.getElementById("address").value;
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            latlng = results[0].geometry.location;
        }
    });

    var iconImg = 'images/Map_Marker.png';
    var options = {
        zoom: 10,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById(map_canvas_id), options);
    var boundary = new google.maps.LatLngBounds();

    for (var i = 0; i < markers.length; i++) {
        var point = new google.maps.Marker(markers[i]);
        point.setMap(map);
        point.setIcon(iconImg);

        var infowindow = new google.maps.InfoWindow(infoWindowContents[i]);
        infowindow.open(map, point);
        maxWidth: 25

        google.maps.event.addListener(point, 'click', function () {
            infowindow.open(map, point);
            maxWidth: 25
        });

        boundary.extend(point.getPosition());
    }

    if (Bound) {
        map.fitBounds(boundary);
    }
    else
        map.setCenter(latlng);


}

function init_map(map_canvas_id, markers, infoWindowContents , Bound) {

    var address = document.getElementById("address").value;
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            latlng = results[0].geometry.location;
        }
    });

     var iconImg = 'images/Map_Marker.png';
     var options = {
         zoom: 10,
         mapTypeId: google.maps.MapTypeId.ROADMAP
     };
     map = new google.maps.Map(document.getElementById(map_canvas_id), options);
     var boundary = new google.maps.LatLngBounds();

     for (var i = 0; i < markers.length; i++) {
         var point = new google.maps.Marker(markers[i]);
         point.setMap(map);
         point.setIcon(iconImg);

         createInfoWindow(map, point, infoWindowContents[i]);
         boundary.extend(point.getPosition());
     }

     if (Bound) {
         map.fitBounds(boundary);
     }
     else
         map.setCenter(latlng);

    
 }

 // This function displays the information window when the marker is clicked
 function createInfoWindow(map, marker,content_marker) {

    var infowindow = new google.maps.InfoWindow(content_marker);

    google.maps.event.addListener(marker, 'click', function () {
        infowindow.open(map, marker);
        maxWidth: 25
    });

 }